package com.rendevous.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.simple.parser.ParseException;

import com.rendevous.bean.Task;
import com.rendevous.dao.BasicDAO;
import com.rendevous.dao.FileDAO;
import com.rendevous.exception.TaskNotExistException;
import com.rendevous.utils.Constants;

/**
 * @author <name>
 * @version 0.1
 * @since 18-09-2012
 * 
 *        Service Tier API, this class provide the interface to manipulate the
 *        todo list.
 * 
 *        Assumptions :
 * 
 */
public abstract class ToDoList {
	protected BasicDAO dao;

	public ToDoList() throws IOException, ParseException {
		this.dao = new FileDAO(Constants.DATA_PATH);
	}

	abstract public String addTask(Task task);

	abstract public String updateTask(long id, Task newTask);

	abstract public String deleteTask(long id);

	abstract public String undo();

	public String switchDataFile() throws IOException, ParseException {
		if (dao.getDataPath().equals(Constants.DATA_PATH)) {
			dao = new FileDAO(Constants.ARCHIVE_PATH);
			return "switched to archive file";
		} else {
			dao = new FileDAO(Constants.DATA_PATH);
			return "switched to data file";
		}
	}

	public String searchByCondition(Map<String, Object> condition) {
		return null;
	}

	public String displayAllTask() throws IOException, ParseException {
		StringBuilder feedback = new StringBuilder();
		List<Task> taskList = dao.getAllTask();
		for (Task task : taskList) {
			feedback.append(task.toString()+"\n");
		}
		return feedback.toString();
	}

	public String completeTask(long id) throws IOException, ParseException {
		try {
			Task task = dao.getTaskById(id);
			return task.toString();
		} catch (TaskNotExistException e) {
			return Constants.MSG_NOT_FOUND;
		}
	}

	public String help() {
		// TODO add help document
		return null;
	}

	public void exit() {
		dao.updateFile();
	}
}
